export default function dragDialog(id) {
    let timmer = setInterval(() => {
        let dom = document.querySelector('#' + id)
        if (dom) {
            let title = dom.querySelector('.layui-layer-title')
            if (title) {
                handleMouseDown(title, dom)
                clearInterval(timmer)
            }
        }
    }, 1000)
}

function handleMouseDown(title, dom) {
    title.addEventListener('mousedown', (e) => {
        let odiv = dom        //获取目标元素
        //算出鼠标相对元素的位置
        let disX = e.clientX - odiv.offsetLeft;
        let disY = e.clientY - odiv.offsetTop;
        document.onmousemove = (e) => {       //鼠标按下并移动的事件
            //用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
            let left = e.clientX - disX;
            let top = e.clientY - disY;
            //绑定元素位置到positionX和positionY上面
            // this.positionX = top;
            // this.positionY = left;
            //移动当前元素
            odiv.style.position = 'absolute';
            odiv.style.left = left + 'px!important';
            odiv.style.top = top + 'px!important';
            odiv.style.bottom = 'auto!important';
            odiv.style.right = 'auto!important';
        };
        document.onmouseup = () => {
            document.onmousemove = null;
            document.onmouseup = null;
        };
    })
}
